package com.lzh.order.service.impl;

import com.lzh.commonutils.CourseInfo;
import com.lzh.commonutils.UserInfo;
import com.lzh.order.client.CourseClient;
import com.lzh.order.client.UserClient;
import com.lzh.order.entity.TOrder;
import com.lzh.order.mapper.TOrderMapper;
import com.lzh.order.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.order.utils.OrderNumUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author lizuhu
 * @since 2021-05-29
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private CourseClient courseClient;
    @Autowired
    private UserClient userClient;

    //创建订单号
    @Override
    public String createOrder(String courseId, String userId) {
        //远程调用服务获取课程和用户信息
        CourseInfo course = courseClient.getCourseByOrder(courseId);
        UserInfo user = userClient.getUserById(userId);
        //创建订单
        TOrder order = new TOrder();
        order.setOrderNo(OrderNumUtils.getOrder());
        order.setCourseId(courseId);
        order.setCourseTitle(course.getTitle());
        order.setCourseCover(course.getCover());
        order.setTeacherName(course.getTeacherName());
        order.setTotalFee(course.getPrice());
        order.setMemberId(userId);
        order.setMobile(user.getMobile());
        order.setNickname(user.getNickname());
        order.setStatus(0);
        order.setPayType(1);
        baseMapper.insert(order);
        //返回订单号
        return order.getOrderNo();
    }
}
